package de.jmda.core.mproc;

import static de.jmda.core.mproc.ProcessingUtilities.getElementsAnnotatedWith;
import static de.jmda.core.mproc.ProcessingUtilities.getRootElements;
import static de.jmda.core.mproc.ProcessingUtilities.isProcessingOver;
import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.util.List;
import java.util.Set;

import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;

import org.junit.Test;

import de.jmda.core.MarkerAnnotationType;
import de.jmda.core.mproc.Processor;

public class JUTProcessorRootElementsAnnotatedElements extends AbstractJUTProcessors
{
	@MarkerAnnotationType private class TestTypeProcessed { }

	@SupportedAnnotationTypes(value = { "de.jmda.core.MarkerAnnotationType" })
	@SupportedSourceVersion(value = SourceVersion.RELEASE_7)
	private class TestProcessor extends Processor
	{
		private Set<? extends Element> rootElements;
		private Set<? extends Element> elementsAnnotatedWithMarkerAnnotationType;

		@Override
		protected boolean process()
		{
			if (false == isProcessingOver())
			{
				rootElements = getRootElements();
				elementsAnnotatedWithMarkerAnnotationType =
						getElementsAnnotatedWith(MarkerAnnotationType.class);
			}

			return false;
		}

		@Override
		protected Set<Class<? extends Annotation>> getSupposedSupportedAnnotationTypes()
		{
			return asSet(MarkerAnnotationType.class);
		}
	}
	
	private TestProcessor processor = new TestProcessor();

	@Override
  protected List<? extends Processor> getProcessors()
  {
		return asList(processor);
  }

	@Test
	public void testRootElementSingle() throws IOException
	{
		assertEquals(
				"unexpected number of root elements", 1, processor.rootElements.size());
	}

	@Test
	public void testAnnotatedElementSingle() throws IOException
	{
		assertEquals(
				"unexpected number of annotated elements",
				1, processor.elementsAnnotatedWithMarkerAnnotationType.size());
	}

	@Test
	public void testNotEqualsAnnotatedElementRootElement() throws IOException
	{
		assertNotEquals(
				"unexpected number of annotated elements",
				processor.rootElements.iterator().next(),
				processor.elementsAnnotatedWithMarkerAnnotationType.iterator().next());
	}
}